package 树;

public class Leedcode1028从先序遍历还原二叉树 {
    private int index = 0;

    public static void main(String[] args) {
        String str = "1-2--3--4-5--6--7";
        Leedcode1028从先序遍历还原二叉树 o = new Leedcode1028从先序遍历还原二叉树();
        TreeNode node = o.preOrderBuild(str, 0);
        o.inOrder(node);
    }
    public TreeNode preOrderBuild(String str,int times) {
        String s = "";
        int t = 0;
        int num = 0;
        while (index < str.length() && str.charAt(index) == '-'){
            index++;
            num++;
        }
        while (index < str.length() && str.charAt(index) != '-'){
            s = s + str.charAt(index);
            index++;
            t++;
        }
        if (num != times){
            index = index - num - t;
            return null;
        }
        TreeNode root = new TreeNode(Integer.parseInt(s));
//        index++;
        root.left = preOrderBuild(str,++times);
        root.right = preOrderBuild(str,times);
        return root;
    }
    public void inOrder(TreeNode root){
        if(root == null){
            return;
        }
        System.out.print(root.val + " ");

        inOrder(root.left);
        inOrder(root.right);
    }
}
